//
//  CarDetailViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/4.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "CarDetailViewController.h"
#import "CarDetailHeaderCell.h"
#import "YanStateTableViewCell.h"
#import "ShangjiaTableViewCell.h"
#import "NoramlCellMode.h"
#import "NoramlTableViewCell.h"
#import "PeiZhiTableViewCell.h"
#import "PeizhiMode.h"
#import "ImageTableViewCell.h"
#import "PhotoMode.h"
#import "CarTableCellTableViewCell.h"
#import "YanCheTableViewCell.h"
#import "ChartDataMode.h"
#import "PersonalDetailViewController.h"
#import "CompanyDetailViewController.h"
#import "ShareView.h"
#import "ShareWithBackView.h"
#import "RootTabViewController.h"
#import "CZTabBar.h"
#import "HeaderModel.h"
#import "CheckCarState.h"
#import "UserModel.h"
#import "YanCheModel.h"
#import "ShangpuMode.h"
#import "IndexCarModel.h"
#import "CarlistCellMode.h"
#import "Puser.h"
#import "ResMode.h"
#import "AddZTCarFromCarDetailController.h"
#import "UserInfoUpdateResde.h"
#import "IndexNavViewController.h"
#import "UpdateCarTableViewController.h"
#import "PostCarTableViewController.h"
#import "bottomButton.h"
#import "UMSocial.h"
#import "Brand.h"
#import "GotoViewController.h"
#import "CommitInfoViewController.h"
#import "MessageDetailTableViewController.h"
#import "HeaderModeFrame.h"
#import "LoadingView.h"

@interface CarDetailViewController ()<UITableViewDataSource,UITableViewDelegate,ShareclickDelegate,ShareWithBackToIndexDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bottomview;
@property (nonatomic,strong) NSMutableArray *noramlarray;
@property (nonatomic,strong) PeizhiMode *mode;
@property (nonatomic,strong) NSMutableArray *photoarray;
@property (nonatomic,strong) CarDetailmodel *carmode;
@property (nonatomic,strong) ChartDataMode *chartmode;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *sectionarray;
@property (nonatomic,strong) Puser *user;
@property (weak, nonatomic) IBOutlet UIView *mybottomview;
@property (weak, nonatomic) IBOutlet UIView *mydownbottomview;
@property (nonatomic,strong) PhotoMode *photomode;
@property (weak, nonatomic) IBOutlet bottomButton *favbuttom;
@property (nonatomic,strong) UIButton *scrollbtn;
@property (nonatomic,strong) LoadingView *loading;

@end

@implementation CarDetailViewController
+(instancetype)viewcontroller{
    CarDetailViewController *detailvc= [XHMianStory instantiateViewControllerWithIdentifier:@"cardetail"];
    detailvc.hidesBottomBarWhenPushed=YES;
    return detailvc;
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
-(NSMutableArray *)photoarray{
    if (_photoarray==nil) {
        _photoarray=[NSMutableArray array];
    }
    return _photoarray;
}

-(LoadingView *)loading{
    if (_loading==nil) {
        _loading=[LoadingView loading];
        _loading.frame=self.tableView.bounds;
        [self.tableView addSubview:_loading];
        _loading.hidden=YES;
    }
    return _loading;
}

-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        return;
    }else{
        NSString *phoneurl=[NSString stringWithFormat:@"tel:%@",self.carmode.ct_phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneurl]];
    }
}

- (IBAction)favclick:(UIButton *)sender {
    
    if (sender.selected) {
        
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在取消";
        
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"action"]=@"cancel_fav";
        params[@"type"]=[self.user.power integerValue] <5?@"person":@"company";
        params[@"user_id"]=[self.user.power integerValue] <5?self.user.user_id:self.user.com_id;
        
        NSArray *IDarray=@[self.carmode.ID];
        params[@"car_id"]=[NSString DataTOjsonString:IDarray];
        
        [PostUitls Post:myfavurl parameters:params success:^(id response) {
            
            UserInfoUpdateResde *resd=[UserInfoUpdateResde resdeWithDict:response];
            hud.mode = MBProgressHUDModeText;
            if ([resd.state integerValue]==400) {
                hud.labelText=@"取消收藏成功";
                sender.selected=NO;
            }else{
                hud.labelText=@"取消收藏失败";
            }
            
            [hud hide:YES afterDelay:1];
        } failure:^(NSError *error) {
            hud.mode = MBProgressHUDModeText;
            hud.labelText=@"网络错误";
            [hud hide:YES afterDelay:1];
        }];
        
        
        
    }else{
        
        
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在收藏";
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"action"]=@"add_fav";
        params[@"type"]=[self.user.power integerValue]<5?@"person":@"company";
        params[@"user_id"]=[self.user.power integerValue]<5?self.user.user_id:self.user.com_id;
        params[@"car_id"]=self.carmode.ID;
        if ([self.user.power integerValue]==8) {
            params[@"parent_id"]=self.user.parent_id;
        }
        
        [PostUitls Post:myfavurl parameters:params success:^(id response) {
            hud.mode=MBProgressHUDModeText;
            ResMode *resdmode=[ResMode modeWithDict:response];
            if ([resdmode.state integerValue]==400) {
                hud.labelText=@"收藏成功";
                sender.selected=YES;
            }else{
                hud.labelText=resdmode.msg;
            }
            [hud hide:YES afterDelay:1];
        } failure:^(NSError *error) {
            hud.mode=MBProgressHUDModeText;
            hud.labelText=[NSString stringWithFormat:@"%@",error];
            [hud hide:YES afterDelay:1];
        }];
        
        
    }
    
}

-(void)buttonclick:(NSInteger)ButtonIndex{
    if (ButtonIndex==0) {
        NSLog(@"回到主页");
        RootTabViewController *roottab=self.navigationController.tabBarController;
        if (roottab.cztabBar.currtitem==0) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [self.navigationController popToRootViewControllerAnimated:NO];
            [roottab.cztabBar setcheckItem:0];
            
        }
        
        
        
        
    }else{
        [self share];
    }
}
-(CarDetailmodel *)carmode{
    if (_carmode==nil) {
        _carmode=[[CarDetailmodel alloc]init];
    }
    return _carmode;
}

-(NSMutableArray *)sectionarray{
    if (_sectionarray==nil) {
        _sectionarray=[NSMutableArray array];
    }
    return _sectionarray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bottomview.layer.borderWidth=1;
    self.bottomview.hidden=YES;
    self.bottomview.layer.borderColor=[UIColorWithRGB(238, 238, 238) CGColor];
    self.mybottomview.layer.borderWidth=1;
    self.mybottomview.layer.borderColor=[UIColorWithRGB(238, 238, 238) CGColor];
    self.mybottomview.hidden=YES;
    self.navigationItem.title=@"车源详情";
    
    ShareWithBackView *shareandback=[ShareWithBackView sharewithback];
    shareandback.delegate=self;
    UIBarButtonItem *rightitem=[[UIBarButtonItem alloc] initWithCustomView:shareandback];
    self.navigationItem.rightBarButtonItem=rightitem;
    
    self.tableView.contentInset=UIEdgeInsetsMake(0, 0, 46, 0);
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if (self.isFromSuS) {
        NSMutableArray *vcarray=[[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-2];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-3];
        self.navigationController.viewControllers = vcarray;
    }
    
    [self setupChart];
    [self setupScrollTopButton];
    
    // Do any additional setup after loading the view.
}

-(void)setupScrollTopButton{
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(XHScreenwidth-45-8, (XHScreenheight-45)*0.5, 45, 45)];
    [button setImage:[UIImage imageNamed:@"scolltotop"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(scrolltotop) forControlEvents:UIControlEventTouchUpInside];
    self.scrollbtn=button;
    
}

-(void)scrolltotop{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y>120) {
        self.scrollbtn.hidden=NO;
    }else{
        self.scrollbtn.hidden=YES;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    IndexNavViewController *vc=self.navigationController;
    [vc setupNavBar:NO];
    
}

-(void)loadnoitem{
    UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(16, (XHScreenheight-120)/2, XHScreenwidth-32, 120)];
    lable.font=[UIFont systemFontOfSize:14];
    lable.textColor=textnormalGary;
    lable.textAlignment=NSTextAlignmentCenter;
    
    lable.text=@"该车源已下架或删除";
    
    [self.tableView addSubview:lable];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
}

-(void)setID:(NSString *)ID{
    _ID=ID;
    [self getCarDetailMode];
}
-(void)getCarDetailMode{
    
    self.loading.hidden=YES;
    
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"where";
    if (self.isformDown) {
        
        params[@"is_sale"]=@"1";
        
    }
    int power=[self.user.power intValue];
    if (power<5) {
        params[@"self_id"]=self.user.user_id;
        params[@"type"]=@"0";
    }else{
        params[@"self_id"]=self.user.com_id;
        if (power ==8) {
            params[@"type"]=@"2";
        }else{
            params[@"type"]=@"1";
        }
    }
    
    params[@"cond"]=[NSString stringWithFormat:@"id=%@",_ID];
    [PostUitls Post:carurl parameters:params success:^(id response) {
        NSDictionary *dict=response;
        IndexCarModel *resmode=[IndexCarModel indexcarWithDcit:dict];
        
        if ([resmode.state integerValue]==400) {
            self.carmode=resmode.data[0];
            [self loadedata];
            [self.tableView reloadData];
        }else{
            [self loadnoitem];
        }
        self.loading.hidden=YES;
        NSLog(@"%@",response);
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    NSMutableDictionary *params2=[NSMutableDictionary dictionary];
    params2[@"action"]=@"add_history";
    
    
    if (power<5) {
        params2[@"type"]=@"person";
        params2[@"user_id"]=self.user.user_id;
    }else{
        params2[@"type"]=@"company";
        params2[@"user_id"]=self.user.com_id;
        if (power==8) {
            params2[@"pid"]=self.user.parent_id;
        }
    }
    params2[@"car_id"]=self.ID;
    [PostUitls Post:myhistoryurl parameters:params2 success:^(id response) {
        NSDictionary *dict=response;
        NSLog(@"%@",dict[@"msg"]);
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
-(void)share{
    NSArray *titlearray=@[@"微信",@"朋友圈",@"短信"];
    UIImage *weixin=[UIImage imageNamed:@"weixin"];
    UIImage *pengyou=[UIImage imageNamed:@"liulan"];
    UIImage *sms=[UIImage imageNamed:@"duanxin"];
    NSArray *icon=@[weixin,pengyou,sms];
    ShareView *share=[[ShareView alloc]initWithTitle:titlearray andIcons:icon delegate:self];
    [share show];
}

- (IBAction)sendmessage:(id)sender {
    if ([self checkPush]) {
        MessageDetailTableViewController *messagevc=[[MessageDetailTableViewController alloc]init];
        messagevc.navigationItem.title=self.carmode.son_name?self.carmode.son_name:self.carmode.shop_name;
        messagevc.toico=self.carmode.ico;
        if ([self.carmode.user_id integerValue]>0) {
            messagevc.toid=self.carmode.user_id;
            messagevc.totype=@"1";
           
            
        }else{
            messagevc.toid=self.carmode.com_id;
            if ([self.carmode.parent_id integerValue]!=0) {
                messagevc.totype=@"3";
            }else{
                messagevc.totype=@"2";
            }
            
        }
        
        [self.navigationController pushViewController:messagevc animated:YES];
        
    }
}


- (IBAction)tell:(id)sender {
    if ([self checkPush]) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"确定拨打此电话么" message:self.carmode.ct_phone delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
}


-(void)shareclickWithIndex:(NSInteger)index{
    NSString *subtitle=[NSString stringWithFormat:@"%@|%@|%@/%@",self.carmode.car_source_type,[self.carmode.timer integerValue]==0?@"期货":@"现货",self.carmode.car_outside_color,self.carmode.car_inside_color];
    if (![self.carmode.stock_position isEqualToString:@""]) {
        subtitle=[NSString stringWithFormat:@"%@|%@",subtitle,self.carmode.stock_position];
    }
    
    if (![self.carmode.sale_area isEqualToString:@""]) {
        subtitle=[NSString stringWithFormat:@"%@|%@",subtitle,self.carmode.sale_area];
    }
    if ([self.carmode.sysprice floatValue]>0) {
        subtitle=[NSString stringWithFormat:@"%@  \n%@万",subtitle,self.carmode.sysprice];
    }else{
        subtitle=[NSString stringWithFormat:@"%@  \n%@",subtitle,@"电议"];
    }
    
    NSString *gourl=[NSString stringWithFormat:@"%@%@%@",websitehosturl,@"/modules.php?app=car_information&cid=",self.carmode.ID];
    
    NSString *url;
    if (self.carmode.imgs.count>0) {
        url=[NSString getImageURLstr:self.carmode.imgs[0]];
    }else{
        url=[NSString getImageURLstr:self.carmode.brand_logo];
    }
    UMSocialUrlResource *resource=[[UMSocialUrlResource alloc]initWithSnsResourceType:UMSocialUrlResourceTypeImage url:url];
    switch (index) {
        case 0:
        {
            
            [UMSocialData defaultData].extConfig.wechatSessionData.url = gourl;
            ;
            [UMSocialData defaultData].extConfig.wxMessageType =UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatSessionData.title =self.carmode.car_name;
            
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:subtitle image:nil location:nil urlResource:resource presentedController:self completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }
            }];
        }
            break;
        case 1:
        {
            
            [UMSocialData defaultData].extConfig.wechatTimelineData.url = gourl;
            ;
            [UMSocialData defaultData].extConfig.wxMessageType =UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatTimelineData.title =self.carmode.car_name;
            
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:subtitle image:nil location:nil urlResource:resource presentedController:self completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }
            }];
        }
            break;
            
        case 2:
        {
            
            
            
            NSString *content=[NSString stringWithFormat:@"%@\n%@\n%@",self.carmode.car_name,subtitle,gourl];
            
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToSms] content:content image:nil location:nil urlResource:resource presentedController:self completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }
            }];
        }
            break;
            
        default:
            break;
    }
    
    
    NSLog(@"%ld",index);
}
-(void)setupChart{
    ChartDataMode *chartmode=[[ChartDataMode alloc]init];
    self.chartmode=chartmode;
    self.chartmode.dataarray=@[@"44",@"33",@"22.4",@"33.5",@"46.9"];
    self.chartmode.titlearray=@[@"3.23-3.29",@"3.30-4.5",@"4.6-4.12",@"4.13-4.19",@"4.20-4.26"];
}

-(void)animationView:(UIView *)view{
    view.alpha=0.0f;
    view.hidden=NO;
    [UIView animateWithDuration:0.5 delay:0.2 options:UIViewAnimationOptionLayoutSubviews animations:^{
        view.alpha=1.0f;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)switchBottombar{
    NSInteger power=[self.user.power integerValue];
    self.favbuttom.selected=[self.carmode.atten integerValue]==1;
    if (power<5) {
        if ([self.carmode.user_id isEqualToString:self.user.user_id]) {
            self.bottomview.hidden=YES;
            
            if (self.isformDown) {
                self.mybottomview.hidden=YES;
                [self animationView:self.mydownbottomview];
            }else{
                self.mydownbottomview.hidden=YES;
                [self animationView:self.mybottomview];
            }
            
        }else{
            self.mybottomview.hidden=YES;
            self.mydownbottomview.hidden=YES;
            [self animationView:self.bottomview];
        }
    }else if(power<9){
        if ([self.carmode.com_id isEqualToString:self.user.com_id]) {
            
            if (self.isformDown) {
                self.mybottomview.hidden=YES;
                [self animationView:self.mydownbottomview];
            }else{
                self.mydownbottomview.hidden=YES;
                [self animationView:self.mybottomview];
            }
            
            
        }else{
            self.mybottomview.hidden=YES;
            self.mydownbottomview.hidden=YES;
            [self animationView:self.bottomview];
        }
        
    }
    
    
}

-(void)loadedata{
    
    [self switchBottombar];
    HeaderModel *mode=[[HeaderModel alloc]init];
    mode.name=self.carmode.car_name;
    mode.spirce=self.carmode.sprice;
    mode.sysprice=self.carmode.sysprice;
    mode.time=self.carmode.addtime;
    mode.con_price=self.carmode.conf_price;
    mode.car_source_type=self.carmode.car_source_type;
    mode.is_price=self.carmode.is_price;
    mode.discount=self.carmode.discount;
    mode.discount_scale=self.carmode.discount_scale;
    HeaderModeFrame *headerframe=[[HeaderModeFrame alloc]init];
    headerframe.headmode=mode;
    
    NoramlCellMode *leixing=[[NoramlCellMode alloc]init];
    leixing.title=@"车源类型";
    leixing.detail=self.carmode.car_source_type;
    [self.noramlarray addObject:leixing];
    
    
    NoramlCellMode *yanse=[[NoramlCellMode alloc]init];
    yanse.title=@"颜色";
    yanse.detail=[NSString stringWithFormat:@"%@/%@",self.carmode.car_outside_color,self.carmode.car_inside_color];
    [self.noramlarray addObject:yanse];
    
    
    NoramlCellMode *yikoujia=[[NoramlCellMode alloc]init];
    yikoujia.title=@"是否一口价";
    switch ([self.carmode.is_price integerValue]) {
        case 0:
            yikoujia.detail=@"一口价";
            break;
        case 1:
            yikoujia.detail=@"可议价";
            break;
        case 2:
            yikoujia.detail=@"电议";
            break;
        default:
            break;
    }

    [self.noramlarray addObject:yikoujia];
    NoramlCellMode *xingzhi=[[NoramlCellMode alloc]init];
    xingzhi.title=@"车源性质";
    xingzhi.detail=[self.carmode.car_nature intValue]==0?@"资源":@"自店";
    [self.noramlarray addObject:xingzhi];
    
    
    NoramlCellMode *paifang=[[NoramlCellMode alloc]init];
    paifang.title=@"排放标准";
    switch ([self.carmode.standard intValue]) {
        case 0:
            paifang.detail=@"不限";
            break;
        case 4:
            paifang.detail=@"国四";
            break;
        case 5:
            paifang.detail=@"国五";
            break;
            
        default:
            break;
    }
    [self.noramlarray addObject:paifang];
    
    
    
    NoramlCellMode *daiqi=[[NoramlCellMode alloc]init];
    daiqi.title=@"货期";
    daiqi.detail=[self.carmode.timer intValue]==0?@"期货":@"现货";
    [self.noramlarray addObject:daiqi];
    
    if (![self.carmode.car_number isEqualToString:@""]) {
        NoramlCellMode *chejia=[[NoramlCellMode alloc]init];
        chejia.title=@"车架号";
        chejia.detail=self.carmode.car_number;
        [self.noramlarray addObject:chejia];
    }
    if (![self.carmode.stock_position isEqualToString:@""]) {
        NoramlCellMode *kuwei=[[NoramlCellMode alloc]init];
        kuwei.title=@"库位";
        kuwei.detail=self.carmode.stock_position;
        [self.noramlarray addObject:kuwei];
    }
    if (![self.carmode.sale_area isEqualToString:@""]) {
        NoramlCellMode *quyu=[[NoramlCellMode alloc]init];
        quyu.title=@"销售区域";
        quyu.detail=self.carmode.sale_area;
        [self.noramlarray addObject:quyu];
    }
    
    if (![self.carmode.fee isEqualToString:@""]) {
        NoramlCellMode *shouxu=[[NoramlCellMode alloc]init];
        shouxu.title=@"手续";
        shouxu.detail=self.carmode.fee;
        [self.noramlarray addObject:shouxu];
    }
    
    if (![self.carmode.car_configure isEqualToString:@""]) {
        PeizhiMode *peizhimode=[[PeizhiMode alloc]init];
        peizhimode.title=@"配置";
        
        peizhimode.longdetail=self.carmode.car_configure;
        [self.noramlarray addObject:peizhimode];
    }
    if (self.carmode.imgs.count>0) {
        PhotoMode *mode=[[PhotoMode alloc]init];
        mode.title=@"图片";
        mode.imgurls=self.carmode.imgs;
        
        [self.noramlarray addObject:mode];
        self.photomode=mode;
    }
    if (self.carmode.vehicle_imgs.count>0) {
        PhotoMode *mode=[[PhotoMode alloc]init];
        mode.title=@"资料";
        mode.imgurls=self.carmode.vehicle_imgs;
        
        [self.noramlarray addObject:mode];
//        self.photomode=mode;
    }
    
    CheckCarState *state=[[CheckCarState alloc]init];
    NSString *statetile;
    NSString *detailtext;
    BOOL ischecked;
    switch ([self.carmode.check_car integerValue]) {
        case 0:
            statetile=@"未验";
            detailtext=@"申请验车";
            ischecked=NO;
            break;
        case 1:
            statetile=@"未验";
            detailtext=@"已申请";
            ischecked=NO;
            break;
        case 2:
            statetile=@"已验";
            detailtext=@"查看详情";
            ischecked=YES;
            break;
        default:
            break;
    }
    state.title=statetile;
    state.detail=detailtext;
    
    Puser *user= [Puser getUser];
    
    BOOL isshow;
    switch ([self.carmode.sale_type integerValue]) {
        case PERSONAL:
            isshow=[self.carmode.user_id isEqualToString:user.user_id];
            break;
        default:
            isshow=[self.carmode.com_id isEqualToString:user.com_id];
            break;
    }
    
    YanCheModel *yan=[[YanCheModel alloc]init];
    yan.isChecked=ischecked;
    
    ShangpuMode *shangpumode=[[ShangpuMode alloc]init];
    shangpumode.phonenumber=self.carmode.ct_phone;
    shangpumode.title=self.carmode.shop_name;
    shangpumode.shop_type=self.carmode.sale_type;
    shangpumode.power=[self.user.power integerValue];
    shangpumode.logo=self.carmode.ico;
    shangpumode.block=^{
        
        if ([self checkPush]) {
            if ([self.carmode.user_id integerValue]!=0) {
                PersonalDetailViewController *cdvc=[PersonalDetailViewController viewcontroller];
                cdvc.user_id=self.carmode.user_id;
                [self.navigationController pushViewController:cdvc animated:YES];
            }else{
                CompanyDetailViewController *cdvc=[CompanyDetailViewController viewcontroller];
                cdvc.com_id=self.carmode.com_id;
                cdvc.parent_id=self.carmode.parent_id;
                [self.navigationController pushViewController:cdvc animated:YES];
            }
            
        }
        
    };
    [self.sectionarray addObject:@[headerframe]];
    [self.sectionarray addObject:self.noramlarray];
    [self.sectionarray addObject:@[shangpumode]];
    if (self.carmode.con_car.count>0) {
        NSMutableArray *mcararray=[NSMutableArray array];
        for (int i=0; i<self.carmode.con_car.count; i++) {
            CarlistCellMode *cellmode=[[CarlistCellMode alloc]init];
            
            cellmode.cardmode=self.carmode.con_car[i];
            [mcararray addObject:cellmode];
        }
        
        [self.sectionarray addObject:mcararray];
    }
    
}

-(BOOL)checkPush{
    int power= [self.user.power intValue];
    
    if (power==1||power==5||power==8) {
        
        return YES;
        
    }else if(power==6||power==2){
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        
        hud.labelText=@"正在审核中";
        hud.mode = MBProgressHUDModeText;
        
        hud.margin = 10.f;
        
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }else if(power<5){
        CommitInfoViewController *gotovc=[CommitInfoViewController   viewcontroller];
        gotovc.typestr=@"查看商家详情";
        gotovc.navigationItem.title=@"商家详情";
        [self.navigationController pushViewController:gotovc  animated:YES];
        return NO;
    }else{
        return NO;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)noramlarray{
    if (_noramlarray==nil) {
        _noramlarray=[NSMutableArray array];
    }
    return _noramlarray;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array=self.sectionarray[section];
    return array.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.sectionarray.count==0) {
        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    }else{
         tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    }
    return self.sectionarray.count;
}
- (IBAction)downcar:(id)sender {
    [self actioncar:@"car_down"];
}
-(IBAction)upclick:(UIButton *)sender{
    switch (sender.tag) {
        case 0:
            [self actioncar:@"car_del"];
            break;
        case 1:
            [self pushtoEidt];
            break;
        case 2:
            [self actioncar:@"car_up"];
            break;
        default:
            break;
    }
}

-(void)pushtoEidt{
    PostCarTableViewController *up=[PostCarTableViewController viewcontroller];
    up.ID=self.carmode.ID;
    [self.navigationController pushViewController:up animated:YES];
}
-(void)actioncar:(NSString *)action{
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.labelText=@"正在操作";
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"user_id"]=[self.user.power integerValue]<5?self.user.user_id:self.user.com_id;
    params[@"type"]=[self.user.power integerValue]<5?@"person":@"company";
    if ([self.user.power integerValue]==8) {
        params[@"pid"]=self.user.parent_id;
    }
    params[@"action"]=action;
    NSMutableArray *IDarray=[NSMutableArray array];
    [IDarray addObject:self.carmode.ID];
    
    params[@"car_id"]=[NSString DataTOjsonString:IDarray];
    
    [PostUitls Post:mycarsource parameters:params success:^(id response) {
        UserInfoUpdateResde *resdmode=[UserInfoUpdateResde resdeWithDict:response];
        if ([resdmode.state integerValue]==400) {
            self.isformDown=[action isEqualToString:@"car_down"]?YES:NO;
            [self switchBottombar];
            if ([action isEqualToString:@"car_del"]) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
        hud.labelText=resdmode.msg;
        hud.mode = MBProgressHUDModeText;
        [hud hide:YES afterDelay:1];
    } failure:^(NSError *error) {
        hud.labelText=@"网络错误";
        hud.mode = MBProgressHUDModeText;
    }];
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, XHScreenheight, 10)];
    view.backgroundColor=UIColorWithRGB(238, 238, 238);
    UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(16, 10, XHScreenheight, 10)];
    [view addSubview:lable];
    lable.font=[UIFont systemFontOfSize:14];
    lable.textColor=textnormalGary;
    lable.text=@"相关车源信息";
    view.height=24;
    if (section==3) {
        return view;
    }else{
        return nil;
    }
    
}
-(void)upcar{
    
}
- (IBAction)addcartoZT:(id)sender {
    
    if ([self checkPush]) {
        AddZTCarFromCarDetailController *advc=[AddZTCarFromCarDetailController viewcontroller];
        advc.cardetail=self.carmode;
        advc.photomode=self.photomode;
        [self.navigationController pushViewController:advc animated:YES];

    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section==3) {
        return @"相关车源信息";
    }else{
        return nil;
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    switch (section) {
            
            
        case 3:
            return 20;
            break;
            
        default:
            return 0;
            break;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            
            NSArray *array=self.sectionarray[indexPath.section];
            NoramlCellMode *mode=array[indexPath.row];
            return mode.rowheight;
        }
            break;
        case 1:
        {
            
            NSArray *array=self.sectionarray[indexPath.section];
            NoramlCellMode *mode=array[indexPath.row];
            if (mode.rowheight!=0) {
                return mode.rowheight;
            }else{
                return 44;
            }
            
        }
            break;
        case 2:
        {
            if (indexPath.row==0) {
                return 80;
                //            }else if(indexPath.row==1)
                //            {
                //                return 569;
                //            }
            }
            else{
                return 44;
            }
            
        }
            break;
            
        case 3:
        {
            NSArray *array=self.sectionarray[indexPath.section];
            CarlistCellMode *mode=array[indexPath.row];
            return mode.rowheight;
            
        }
            break;
        default:
            return 44;
            break;
    }
}




- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section==1) {
        NSArray *array=self.sectionarray[indexPath.section];
        NoramlCellMode *mode=array[indexPath.row];
        if ([mode isKindOfClass:[PeizhiMode class]]) {
            return YES;
        }else{
            return NO;
        }

    }
    return NO;
   
}

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  
    if (action == @selector(copy:)) {
        
        return YES;
       
    }
  
    return NO;

}

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
   
    if (action == @selector(copy:)) {
        NSArray *array=self.sectionarray[indexPath.section];
         NoramlCellMode *mode=array[indexPath.row];
        if ([mode isKindOfClass:[PeizhiMode class]]) {
            PeizhiMode *pezhimode=mode;
            [UIPasteboard generalPasteboard].string = pezhimode.longdetail;
        }
        
      
    }
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 1:{
//            NSArray *array=self.sectionarray[indexPath.section];
//            NoramlCellMode *mode=array[indexPath.row];
//            if ([mode isKindOfClass:[PeizhiMode class]]) {
//                
//                UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
//                [cell becomeFirstResponder];
//                UIMenuController *menu = [UIMenuController sharedMenuController];
//                [menu setTargetRect:cell.frame inView:cell.superview];
//                [menu setMenuVisible:YES animated:YES];
//            }
  
        }
            break;
            
        case 2:
        {NSArray *array=self.sectionarray[indexPath.section];
            
            NoramlCellMode *mode=array[indexPath.row];
            if (mode.block) {
                mode.block();
            }
        }
            break;
        case 3:
        {
            CarDetailViewController *detailvc=[CarDetailViewController viewcontroller];
            NSArray *array=self.sectionarray[indexPath.section];
            CarlistCellMode *cellmode=array[indexPath.row];
            detailvc.ID=cellmode.cardmode.ID;
            [self.navigationController pushViewController:detailvc animated:YES];
        }
            break;
        default:
            break;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.sectionarray[indexPath.section];
    switch (indexPath.section) {
        case 0:
        {
            CarDetailHeaderCell *cell=[CarDetailHeaderCell cell];
            
            cell.mode=array[0];
            return cell;
        }
            break;
            
        case 1:
        {
            NoramlCellMode *mode=array[indexPath.row];
            
            if ([mode isKindOfClass:[PeizhiMode class]]) {
                PeiZhiTableViewCell *cell=[PeiZhiTableViewCell cell];
                PeizhiMode *peizhimode=mode;
                cell.detailtext.text=peizhimode.longdetail;
                cell.titlelabel.text=peizhimode.title;
               
                return cell;
            }else if([mode isKindOfClass:[PhotoMode class]])
            {
                PhotoMode *photomode=mode;
                ImageTableViewCell *cell=[ImageTableViewCell cellWithTableview:tableView];
                cell.mode=mode;
                return cell;
            }
            
            else{
                NoramlTableViewCell  *cell=[NoramlTableViewCell cellWithTableView:tableView];
                cell.textLabel.text=mode.title;
                cell.detailTextLabel.text=mode.detail;
                return cell;
            }
            
        }
            break;
            
        case 2:
        {
            //            if (indexPath.row==0) {
            //                CheckCarState *state=array[0];
            //                YanStateTableViewCell *cell=[YanStateTableViewCell cellWithTableView:tableView];
            //                cell.titlelabe.text=state.title;
            //                [cell.detailbtn setTitle:state.detail forState:UIControlStateNormal];
            //                  return cell;
            //           }
            //                else if(indexPath.row==1){
            //                YanCheModel *yanche=array[indexPath.row];
            //                YanCheTableViewCell *cell=[YanCheTableViewCell cellWithTabelview:tableView];
            //                cell.datamodel=self.chartmode;
            //                cell.mode=yanche;
            //                return cell;
            //            }else{
            
            ShangpuMode *mode=array[indexPath.row];
            ShangjiaTableViewCell *cell=[ShangjiaTableViewCell cellWithTableView:tableView];
            cell.mode=mode;
            return cell;
            //            }
            
            
        }
            break;
        case 3:
        {
            CarlistCellMode *cellmode=array[indexPath.row];
            
            CarTableCellTableViewCell *cell=[CarTableCellTableViewCell cellWithTableView:tableView];
            cell.carcellmode=cellmode;
            return cell;
            
        }
            break;
            
        default:
            return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            break;
    }
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
